<?php
/**
 * Eren Framework
 * 
 * The adapter of Session operation.
 * 
 * @category System
 * @author Manu Soros Leu
 * @copyright manusoros@gmail.com
 */

class Eren_Session_Adapter
{
    
    /**
     * The instance of Session class like 'Eren_Session_Default'
     *
     * @var Eren_Session_Abstract
     */
    protected $_session = NULL;
    
    /**
     * Init class
     * 
     * @param  Eren_Session_Abstract  $session  Instance of Session class which inherit from Eren_Session_Abstract
     * @throws Eren_Session_Exception
     */
    public function __construct($session)
    {
        if(!Eren_Validator::isBaseClass($session, 'Eren_Session_Abstract')){
            throw new Eren_Session_Exception('The param of construct of Eren_Session_Adapter must inherit from Eren_Session_Abstract');
        }
        $this->_session = $session;
    }
    
    /**
     * Get SessionID
     * 
     * @return string
     */
    public function getSessionID()
    {
        return $this->_session->getSessionID();
    }
    
    /**
     * Set Session Key and Value
     * 
     * @param string $key
     * @param int|string|array|object $value
     * @throws Eren_Session_Exception
     */
    public function set($key, $value)
    {
        if(is_array($key) || empty($key)){
            throw new Eren_Session_Exception('The first param of Eren_Session::set cant be array or null');
        }
        $key = (string) $key;
           $this->_session->set($key, $value);
    }
    
    /**
     * Get Session By Key
     * 
     * @param string $key
     * @param int|string|array|object $default
     * @throws Eren_Session_Exception
     */
    public function get($key, $default = NULL)
    {
        if(is_array($key) || empty($key)){
            throw new Eren_Session_Exception('The first param of Eren_Session::get cant be array or null');
        }
        $key = (string) $key;
        return $this->_session->get($key, $default);
    }
    
    /**
     * Get ALl Sessions
     * 
     * @throws Eren_Session_Exception
     * @return array;
     */
    public function getAll()
    {
        return $this->_session->getAll();
    }
    
    /**
     * Delete Session By Key
     * 
     * @param string $key
     * @throws Eren_Session_Exception
     */
    public function delete($key)
    {
        if(is_array($key) || empty($key)){
            throw new Eren_Session_Exception('The param of Eren_Session::delete cant be array or null');
        }
        $key = (string) $key;
        $this->_session->delete($key);
    }
    
    /**
     * Delete All Session
     * 
     * @throws Eren_Session_Exception
     */
    public function delAll()
    {
        $this->_session->delAll();
    }
}